package cn.zmwh.portal.modules.index.service;

import cn.hutool.core.collection.CollUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.portal.modules.index.mapper.TlTopicMapper;
import cn.zmwh.portal.modules.index.mapper.TlUserCircleFollowMapper;
import cn.zmwh.portal.modules.index.pojo.entity.TlCircle;
import cn.zmwh.portal.modules.index.pojo.entity.TlTopic;
import cn.zmwh.portal.modules.index.pojo.entity.TlUserCircleFollow;
import cn.zmwh.portal.modules.index.pojo.vo.TlCircleVo;
import cn.zmwh.portal.modules.index.pojo.vo.TlTopicVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2024-09-22
 */
@Service
public class TlTopicService extends ServiceImpl<TlTopicMapper, TlTopic> {

    @Autowired
    TlCircleService tlCircleService;
    @Autowired
    TlUserCircleFollowMapper tlUserCircleFollowMapper;

    public Result<List<TlTopicVo>> topicCircle(Long tlId) {
        List<TlTopic> tlTopics = baseMapper.selectList(Wrappers.<TlTopic>lambdaQuery()
                .eq(TlTopic::getOrigin, 1)
                .eq(TlTopic::getShow, 1)
                .orderByAsc(TlTopic::getSort));
        if (CollUtil.isEmpty(tlTopics)) {
            return Result.ok(new ArrayList<>());
        }
        Set<String> topicCodes = tlTopics.stream().map(TlTopic::getCode).collect(Collectors.toSet());

        List<TlCircle> list = tlCircleService.list(Wrappers.<TlCircle>lambdaQuery()
                .in(TlCircle::getTopicCode, topicCodes)
                .orderByDesc(TlCircle::getSort));
        Map<String, List<TlCircleVo>> mapByTopicCode = new HashMap<>();
        if (CollUtil.isNotEmpty(list)) {
            Set<Long> circleIds = list.stream().map(TlCircle::getId).collect(Collectors.toSet());
            Set<Long> followCircleIds = new HashSet<>();
            if (tlId != null) {//查询用户是否关注了
                List<TlUserCircleFollow> tlUserCircleFollows = tlUserCircleFollowMapper.selectList(Wrappers.<TlUserCircleFollow>lambdaQuery()
                        .in(TlUserCircleFollow::getCircleId, circleIds)
                        .eq(TlUserCircleFollow::getUid, tlId));
                if (CollUtil.isNotEmpty(tlUserCircleFollows)) {
                    followCircleIds = tlUserCircleFollows.stream().map(TlUserCircleFollow::getCircleId).collect(Collectors.toSet());
                }
            }

            for (TlCircle item : list) {
                Long id = item.getId();
                TlCircleVo tlCircleVo = new TlCircleVo();
                if (tlId != null && CollUtil.isNotEmpty(followCircleIds) && followCircleIds.contains(id)) {
                    tlCircleVo.setFollow(1);
                }
                BeanUtils.copyProperties(item, tlCircleVo);
                String topicCode = item.getTopicCode();
                List<TlCircleVo> orDefault = mapByTopicCode.getOrDefault(topicCode, new ArrayList<>());
                orDefault.add(tlCircleVo);
                mapByTopicCode.put(topicCode, orDefault);
            }
        }
        List<TlTopicVo> vos = new ArrayList<>();
        for (TlTopic item : tlTopics) {
            TlTopicVo vo = new TlTopicVo();
            BeanUtils.copyProperties(item, vo);
            List<TlCircleVo> circleVos = mapByTopicCode.get(item.getCode());
            vo.setList(circleVos);
            vos.add(vo);
        }
        return Result.ok(vos);
    }
}
